export interface FileNode {
  type: 'file',
  name: string
}

export interface FolderNode {
  type: 'folder',
  name: string,
  children: Array<FileNode|FolderNode>
}

const tree: FolderNode = {
  type: 'folder',
  name: '根目录',
  children: [
    {
      type: 'folder',
      name: '音频',
      children: [
        {
          type: 'folder',
          name: '第一单元',
          children: [
            {
              type: 'file',
              name: 'audio1.mp3'
            },
            {
              type: 'file',
              name: 'audio2.mp3'
            },
            {
              type: 'file',
              name: 'audio3.mp3'
            }
          ]
        },
        {
          type: 'folder',
          name: '第二单元',
          children: [
            {
              type: 'file',
              name: 'audio1.mp3'
            },
            {
              type: 'file',
              name: 'audio2.mp3'
            },
            {
              type: 'file',
              name: 'audio3.mp3'
            }
          ]
        },
        {
          type: 'file',
          name: 'audio1.mp3'
        },
        {
          type: 'file',
          name: 'audio2.mp3'
        }
      ]
    },
    {
      type: 'folder',
      name: '视频',
      children: [
        {
          type: 'folder',
          name: '第一单元',
          children: [
            {
              type: 'file',
              name: 'video1.mp4'
            },
            {
              type: 'file',
              name: 'video2.mp4'
            },
            {
              type: 'file',
              name: 'video3.mp4'
            }
          ]
        },
        {
          type: 'folder',
          name: '第二单元',
          children: [
            {
              type: 'file',
              name: 'video1.mp4'
            },
            {
              type: 'file',
              name: 'video2.mp4'
            },
            {
              type: 'file',
              name: 'video3.mp4'
            }
          ]
        },
        {
          type: 'file',
          name: 'video1.mp4'
        }
      ]
    },
    {
      type: 'file',
      name: 'audio1.mp3'
    },
    {
      type: 'file',
      name: 'video1.mp4'
    },
    {
      type: 'file',
      name: 'video2.mp4'
    }
  ]
};

function pathToNodeNames(path: string) {
  return path.trim().replace(/^\/+|\/+$/g, '').split('/').reduce((all, name) => {
    if (name) {
      all.push(name);
    }
    return all;
  }, [] as string[]);
}

function findNode(path: string): FileNode | FolderNode | null {
  const names = pathToNodeNames(path);
  let node: FileNode | FolderNode | null = tree;
  while (node?.type === 'folder' && names.length !== 0) {
    const name = names.shift();
    let childNode: FileNode | FolderNode | null = null;
    for (let child of node.children) {
      if (child.name === name) {
        childNode = child;
        break;
      }
    }
    node = childNode;
  }
  return names.length === 0 ? node : null;
}

export default {
  listChildren(path: string): Promise<Array<FileNode | FolderNode>> {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const node = findNode(path);
        if (node?.type !== 'folder') {
          reject(new Error('指定文件夹不存在'));
        } else {
          resolve(JSON.parse(JSON.stringify(node.children)));
        }
      }, 200);
    });
  }
}